package com.ftm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ftm.entity.GoodsType;
import com.ftm.entity.User;
import com.ftm.service.GoodsTypeService;
import com.ftm.service.ShopService;
import com.ftm.service.impl.GoodsTypeServiceImpl;
import com.ftm.service.impl.ShopServiceImpl;
import com.ftm.service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserLoginService
 */
@WebServlet("/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserLoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("login".equals(op)) {
			String uinfo=request.getParameter("userinfo");
			if("user".equals(uinfo)) {
				UserLogin(request, response);
			}else if("shop".equals(uinfo)) {
				ShopLogin(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 用户登录方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void UserLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uname = request.getParameter("username");
		String upwd = request.getParameter("userpwd");
		UserServiceImpl us = new UserServiceImpl();
		User user = us.ULogin(uname, upwd);
		if (user!=null) {
			// 登录成功跳转
			request.getSession().setAttribute("user", user.getUserid());
			request.getRequestDispatcher("UserIndex.jsp").forward(request, response);
		} else {
			response.getWriter().append(
					"<script>alert(\"用户名或密码错误\");location.href='http://localhost:8080/ForTheMorning/UserLogin.jsp';</script>");
		}
	}
	/**
	 * 店主登录方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void ShopLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uname = request.getParameter("username");
		String upwd = request.getParameter("userpwd");
		ShopService ss=new ShopServiceImpl();
		boolean flag=ss.SLogin(uname, upwd);
		if (flag) {
			// 登录成功跳转
			//商铺id   ,商铺管理系统使用   zzp
			int shopid=ss.queryOneShop(uname).getShopid();
			request.getSession().setAttribute("shopid", shopid);
			
			request.getSession().setAttribute("shopuser", uname);
			request.getRequestDispatcher("product_list.jsp").forward(request, response);
		} else {
			response.getWriter().append(
					"<script>alert(\"用户名或密码错误\");location.href='http://localhost:8080/ForTheMorning/UserLogin.jsp';</script>");
		}
	}
}
